<div class="layui-card-header layui-card">
    <span class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">首页</a>
        <a><cite>应用列表</cite></a>
    </span>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">应用列表信息</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-card-body">
                    <div style="padding-bottom: 10px;">
                        <button class="layui-btn layuiadmin-btn-list" data-type="add">添加</button>
                    </div>
                </div>
                <table class="layui-hide" id="tList" lay-filter="tList"></table>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="payBar">
    {{# if(d.mchType == 1){  }}
    <a class="layui-btn layui-btn-xs" lay-event="update">修改</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail">详情</a>
    <!--<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="qrcode">二维码管理</a>-->
    {{# }else if(d.mchType==2){  }}
    <a class="layui-btn layui-btn-xs" lay-event="update">修改</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail">详情</a>
    <!--<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="qrcode">二维码管理</a>-->
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="channelList">支付渠道配置</a>
    {{# }else{          }}
    <a class="layui-btn layui-btn-xs" lay-event="update">修改</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail">详情</a>
    {{# }    }}
</script>


<script>
    layui.use(['table','util','admin','form','util'], function(){
        var table = layui.table
        ,$ = layui.$
            ,view =layui.view,
            form =layui.form,
            admin =layui.admin,
            layer =layui.layer
        ,element = layui.element;

        element.render('breadcrumb', 'breadcrumb');//渲染导航信息

        var tplStatus = function(d){
            if(d.state == 0) {
                return "<span class='layui-btn layui-btn-xs layui-btn-disabled'>停止使用</span>";
            }
            return "<span class='layui-btn layui-btn-xs'>使用中</span>";
        };

        table.render({
            elem: '#tList'
            ,url: layui.setter.baseUrl + '/app/list'
            ,where: {
                access_token: layui.data(layui.setter.tableName).access_token
            }
            ,id:'tableReload'
            ,page: 'true'
            ,cols: [[
                {type:'checkbox'}
                ,{field:'appId', title: '应用ID', sort: true }
                ,{field:'appName', title: '应用名称' }
                ,{field:'status',  title: '应用状态',templet: tplStatus }
                ,{field:'createTime',  title: '创建时间', templet: '<div>{{ layui.util.toDateString(d.createTime) }}</div>' }
                ,{field:'edit',title: '操作', width :320,toolbar: '#payBar' }
            ]]
            ,skin: 'line'
        });

        //监听工具条
        table.on('tool(tList)', function(obj) {

            var data = obj.data;

            if (obj.event === 'update') {
                // 1 ajax 查最新数据，
                var curAppId =data.appId;
                admin.req({
                    type: 'get',
                    url: layui.setter.baseUrl + '/app/get',
                    data: {
                        appId : curAppId
                    },
                    error: function(err){
                        layer.alert(err, {
                            title: 'error'
                        })
                    },
                    success: function(res){
                        if(res.code == '0'){
                            admin.popup({
                                title: '修改应用'
                                , area: ['420px', '270px']
                                , id: 'LAY-popup-content-view'
                                , success: function (layero, index) {
                                    view(this.id).render('account/app/update').done(function () {
                                        form.render(null, 'layuiadmin-app-form-list');
                                        var body =layer.getChildFrame('body',index);
                                        body.find('#email').val(data.email);
                                        console.log(body)
                                        form.val("layuiadmin-app-form-list", {

                                            "appName" : data.appName

                                        })
                                        if(res.data.state == 1) {
                                            $("input[name='state'][value='1']").attr("checked",true);
                                        }else {
                                            $("input[name='state'][value='0']").attr("checked",true);
                                        }

                                        form.render();
                                    });
                                }
                            });
                            form.render();
                            form.on('submit(btnM)', function(data){
                                admin.req({
                                    type: 'post',
                                    url: layui.setter.baseUrl + '/app/update', //模拟接口,
                                    // data: "params=" + JSON.stringify(data.field),
                                    data: {
                                        appId: curAppId,
                                        appName: data.field.appName,
                                        state: data.field.state
                                    },
                                    error: function(err){layer.alert(err.msg,{title:"请求失败"})},
                                    success: function(res){
                                        if(res.code == 0){
                                            /*layer.alert(res.msg,{title: "保存成功"},function(index){
                                                table.reload('tableReload');
                                                layer.closeAll();
                                            })*/
                                            layer.msg('保存成功', {
                                                offset: '15px'
                                                ,icon: 1
                                                ,time: 1000
                                            }, function(){
                                                table.reload('tableReload');
                                                layer.closeAll();
                                            });
                                        }else {
                                            /*layer.alert(res.msg,{title: '保存失败'})*/
                                            layer.msg('保存失败', {
                                                offset: '15px'
                                                ,icon: 1
                                                ,time: 1000
                                            });
                                        }
                                    }
                                })
                                return false;
                            });
                        }else {
                            layer.alert(res.msg,{title: '请求失败'})
                        }
                    }
                })


            } else if (obj.event === 'detail') {
                var creId =data.appId;
                admin.req({
                    type: 'get',
                    url: layui.setter.baseUrl + '/app/get',
                    data: {
                        appId: creId,
                    },
                    error: function(err){
                        layer.alert(err);
                    },
                    success: function(res){
                        if(res.code == 0){

                            admin.popup({
                                title: '查看应用'
                                , area: ['450px', '450px']
                                , id: 'LAY-popup-content-edit'
                                , success: function (layero, index) {
                                    view(this.id).render('account/app/detail').done(function () {
                                        form.render(null, 'layuiadmin-app-form-list');
                                        var body =layer.getChildFrame('body',index);
                                        body.find('#email').val(data.email);
                                        console.log(body)

                                        form.val("layuiadmin-app-form-list", {
                                            "appId":data.appId
                                            ,"appName": data.appName
                                            ,"state":data.state ==0?"停止使用":"使用中",
                                            "remark":data.remark,
                                            "createTime":layui.util.toDateString(res.data.createTime),
                                            "updateTime":layui.util.toDateString(res.data.updateTime)
                                        })
                                        form.render();
                                    });
                                }
                            });

                        }else {
                            layer.alert('请求数据失败');
                        }
                    }
                })
            } else if (obj.event === 'qrcode') {
                location.href = layui.setter.baseLocal + "account/qrcode/list/appId=" + data.appId;
            } else if (obj.event === 'channelList') {
                location.href = layui.setter.baseLocal + "account/app/channelList/appId=" + data.appId;
            }
        });



        var $ =layui.$, active ={
            add: function (othis) {
                admin.popup({
                    title: '新建应用'
                    , area: ['395px', '205px']
                    , id: 'LAY-popup-content-create'
                    , success: function (layero, index) {
                        view(this.id).render('account/app/create').done(function () {
                            form.render(null, 'layuiadmin-app-form-list');

                            //监听提交
                            form.on('submit(btnC)', function(data){
                                admin.req({
                                    type: 'post',
                                    url: layui.setter.baseUrl + '/app/add', //模拟接口,
                                    data: data.field,
                                    error: function(err){layer.alert(err.msg,{title:"请求失败"})},
                                    success: function(res){
                                        if(res.code == 0){
                                            /*layer.alert(res.msg,{title: "保存成功"},function(index){
                                                table.reload('tableReload');
                                                layer.closeAll();
                                            })*/
                                            layer.msg('保存成功', {
                                                offset: '15px'
                                                ,icon: 1
                                                ,time: 1000
                                            }, function(){
                                                table.reload('tableReload');
                                                layer.closeAll();
                                            });
                                        }else {
                                            /*layer.alert(res.msg,{title: '新建应用失败'})*/
                                            layer.msg('新建应用失败', {
                                                offset: '15px'
                                                ,icon: 1
                                                ,time: 1000
                                            });
                                        }

                                    }
                                })
                                return false;
                            });

                            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                        });
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-list').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>